<template>
  <div>
    <h1>我的资产</h1>
  </div>
  <div>
    <div class="mb-4">
      <el-button type="info" @click="openDialog()">存钱宝</el-button>
      <el-button type="warning" @click="gotojiebei()">借呗</el-button>
      <el-button type="danger">我的待还</el-button>
    </div>
  </div>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="total" label="总资产" width="180" />
    <el-table-column prop="saveMoney" label="存钱宝" />
    <el-table-column prop="yield" label="预计收益" />
    <el-table-column prop="owe" label="负债" />
    <el-table-column prop="borrow" label="借呗" />
  </el-table>
  <div>
    <el-dialog v-model="dialogFormVisible" title="Shipping address" width="500">
      <el-form :model="form">
        <el-form-item label="我的余额" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="预计收益" :label-width="formLabelWidth">
          <el-input v-model="form.yield" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">Cancel</el-button>
          <el-button type="primary" @click="dialogFormVisible = false">
            Confirm
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
const tableData = ref([]);
import { reactive, ref, onMounted } from "vue";
import { assetList } from "../api/api";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const asslist = ref({});
const router = useRouter();
let userId = ref("");
const listHH = () => {
  console.log("当前userId" + userId);
  assetList(userId).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data;
      asslist.value = res.data.data.total;
      sessionStorage.setItem("asset", JSON.stringify(assetList));
    }
  });
};

const gotojiebei = () => {
  router.push("/gotojiebei");
};

const openDialog = () => {
  router.push("/getMoneyList");
  //   dialogFormVisible.value = true;
  //   let a = sessionStorage.getItem("asset");
  //   //   console.log(u);
  //   if (a != null) {
  //     let asset = JSON.parse(a);
  //     console.log("总资产" + asset);
  //     form.name = asset;
  //   }
};

const form = reactive({
  name: "",
  yield: "",
});
// 页面加载钩子函数
onMounted(() => {
  let u = sessionStorage.getItem("user");
  //   console.log(u);
  if (u != null) {
    let user = JSON.parse(u);
    console.log(user);
    userId = user.id;
    console.log("当前userId" + userId);
  }

  listHH();
});
</script>
